package org.lib.hadoop.hello;

import java.net.URI;
import java.util.Objects;
import java.util.function.Consumer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.protocol.ClientProtocol;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * DFSClient测试
 * @author ben
 * @date 2021-12-14 19:51:06 CST
 */
//@Component
@Slf4j
public class DfsClientRunner implements CommandLineRunner {

	private static Consumer<Object> cs = System.out::println;
	
	private static String haddr = "hdfs://localhost:19870";
	
	@Override
	public void run(String... args) throws Exception {
		Configuration hconf = new Configuration();
		DFSClient dc = new DFSClient(new URI(haddr), hconf);
		
		inspectDfsClient(dc);
		
	}

	/**
	 * 检查DFSClient对象
	 * @author ben
	 * @date 2021-12-14 20:00:55 CST
	 * @param dc
	 */
	private void inspectDfsClient(DFSClient dc) {
		cs.accept("\ninspectDfsClient:");
		cs.accept("dc=" + dc);
		if (Objects.nonNull(dc)) {
			try {
				cs.accept("dc1=" + dc.getClientName());
				cs.accept("dc2=" + dc.getCorruptBlocksCount());
				cs.accept("dc3=" + dc.getLowRedundancyBlocksCount());
				cs.accept("dc4=" + dc.getMissingBlocksCount());
				cs.accept("dc5=" + dc.getRefreshReadBlkLocationsInterval());
				cs.accept("dc6=" + dc.getClientContext());
				cs.accept("dc7=" + dc.getDiskStatus());
				cs.accept("dc8=" + dc.getLeaseRenewer());
				
				ClientProtocol nn = dc.getNamenode();
				cs.accept("nn=" + nn);
				if (Objects.nonNull(nn)) {
					// 全部发生异常！
					// java.lang.IllegalStateException: null
//					cs.accept(nn.getHAServiceState());
//					cs.accept(Arrays.asList(nn.getStats()));
//					cs.accept(nn.getErasureCodingCodecs());
				}
			} catch (Exception e) {
				log.error("发生异常：e=", e);
			}
		}
	}

}
